﻿
(function($) {

    // Utilitys
    window.parseXmlString = function(commits) {
        var xmlstr = "";
        jQuery.map(commits, function(commit) {
            if ($(">Selected", commit).text() == "true") {
                commit = $(commit).clone().get(0);
                $(commit).find(">Children").remove();
                xmlstr += (new zXMLSerializer()).serializeToString(commit);
            }
        });
        return "<ChildrenList>" + xmlstr + "</ChildrenList>";
    }

    // Attributes
    window.VENDOR_ID = null;
    window.VENDOR_TYPE = null;
    window.VENDOR_LANG = 9;
    window.EQUIV_SELECTED_CHECKED = { from: "Selected", to: "checked" };
    window.EQUIV_MEMO_VALUE = { from: "Memo", to: "value" };
    window.EQUIV_FN_VISIBLE = { from: "Selected", to: function(val) { return $(this).is(":visible") } };
    window.EQUIV_FN_ENABLED = { from: "Selected", to: function(val) { return $(this).is(":enabled") } };
    window.EQUIV_FN_VISIBLE_ENABLED = { from: "Selected", to: function(val) { return $(this).is(":visible:enabled") } };
    window.EQUIV_FN_VISIBLE_ENABLED_CHECKED = { from: "Selected", to: function(val) { return $(this).is(":visible:enabled:checked") } };
    window.CHECK_BIND_REGION_NULL = { from: [null, "", "00000000-0000-0000-0000-000000000000"] };
    window.CHECK_COMMIT_NODEID_NULL = { from: [null, ""], correct: function() { $(this).prepend("<option value='00000000-0000-0000-0000-000000000000'>" + I18N.Select + "</option>") } };
    window.EQUIV_STATIC_BIND = function(from, value) {
        return { from: from, to: function() { return value }, doing: "commit" };
    };
    window.EQUIV_FORMAT_DATE = { from: function(val) { return (val.indexOf('T') > 0) ? val.split('T')[0] : val; } };
    window.EQUIV_FORMAT_PRICE = { from: function(val) { return (val != null && val.length > 0) ? parseInt(val) : val; } };
    window.ACTION_TO_SAVE = function(val) {
        if (val)
            $(this).attr("_value", val);
        else
            return $(this).val();
    };
    window.ACTION_GET_TREE = function(cid, rgcid, action) {
        return function(callback) {
            $.get("merchant.axd", { CId: cid || VENDOR_ID, Action: action || "GCGC", RGCId: rgcid, LangId: VENDOR_LANG, format: "xml" }, function(data) {
                callback(data);
            });
        }
    };
    window.ACTION_GET_REGION_TREE = function(id) {
        return function(callback) {
            $.get("traveltongcommondata.axd", { GCId: id || VENDOR_ID, Action: "GGC", RGCId: "50649B52-D91C-DE11-B247-001E68C9B26A", PLevel: 0, Level: 2, LangId: VENDOR_LANG, format: "xml" }, function(data) {
                callback(data);
            });
        }
    };
    window.ACTION_GET_CATEGORY_TREE = function(id) {
        return function(callback) {
            $.get("traveltongcommondata.axd", { GCId: id || VENDOR_ID, Action: "GGC", RGCId: "4BC9BFE3-8D1E-DE11-9504-000000004188", Level: 1, LangId: VENDOR_LANG, format: "xml" }, function(data) {
                callback(data);
            });
        }
    };
    window.ACTION_GET_MEDIA_TREE = function(tid, id) {
        return function(callback) {
            $.get("merchant.axd", { CID: id || VENDOR_ID, Action: "GMDBCIDTID", TID: tid || "36910200-2333-DE11-9504-000000004188", LangId: VENDOR_LANG, format: "xml" }, function(data) {
                callback(data);
            });
        }
    };
    window.ACTION_COMMIT = function(cid, rgcid, action, parser, callback) {
        return function(commits) {
            var stdata = (parser || parseXmlString).call(this, commits); // to parse
            $.post("merchant.axd", { CId: cid || VENDOR_ID, Action: action || "UCGC", RGCId: rgcid, LangId: VENDOR_LANG, STData: stdata }, callback);
        }
    };
    window.EVENT_CLICK_REFRESH = { click: "refresh" };
    window.EVENT_COMMITED = function() { };
    window.ACTION_DO_COMMIT = function(type) {
        if (type == "register")
            window.EVENT_COMMITED = HANDLER_REGISTERED;
        else if (type == "change")
            window.EVENT_COMMITED = HANDLER_CHANGED;
        treeforms.binder.commitAll();
        $(".form_panel>.bottom>.submit").val(I18N.wait_please).attr("disabled", "disabled").prev(".loading").show()
            .prev(".finish").hide();
    };
    window.HANDLER_REGISTERED = function(result) {
        location.href = "./?action=welcome" +
        "&domain=" + encodeURIComponent($("Domain", result).text()) +
        "&name=" + encodeURIComponent($("#company").val());
    };
    window.HANDLER_CHANGED = function(result) {
        //location.href = "";
        $(".form_panel>.bottom>.submit").val(I18N.edit).removeAttr("disabled").prev(".loading").hide()
            .prev(".finish").show();
    };
    window.REGION_SELECTOR = function(container, country, state, city) {

        function useDefault(element) {
            if ($(element, container).children("option:contains('" + I18N.Select + "')").length == 0)
                $(element, container)
                        .prepend("<option value='00000000-0000-0000-0000-000000000000' selected>" + I18N.Select + "</option>")
                        .val("00000000-0000-0000-0000-000000000000")
                        .each(function() { $(this).triggerHandler("change") });
        }

        var countryid = $(country, container).attr("_value");
        var stateid = $(state, container).attr("_value");
        var cityid = $(city, container).attr("_value");
        useDefault(country + "," + state + "," + city);
        new treeforms.binder({
            name: "国家列表" + country,
            container: container,
            tree: ACTION_GET_REGION_TREE("50649B52-D91C-DE11-B247-001E68C9B26A"),
            linkings: { from: "/ResponseData/Children/Children", to: country, outputs: { from: ["GenericCategoryId", "Name"], to: "<option value='{0}'>{1}</option>", empty: false} },
            ready: function() {
                useDefault(country);
                if (countryid && countryid != "") {
                    $(country, container).val(countryid);
                    countryid = null;
                }
                $(country, container).change(function() {
                    if ($(this).val() == "00000000-0000-0000-0000-000000000000") {
                        $(state + "," + city, container).hide();
                    } else
                        new treeforms.binder({
                            name: "省份列表" + state,
                            container: container,
                            tree: ACTION_GET_REGION_TREE($(this).val()),
                            linkings: { from: "/ResponseData/Children", to: state, outputs: { from: ["GenericCategoryId", "Name"], to: "<option value='{0}'>{1}</option>"} },
                            ready: function() {
                                useDefault(state);
                                if (stateid && stateid != "") {
                                    $(state, container).val(stateid);
                                    stateid = null;
                                }
                                if ($(state, container).children("option").length == 1)
                                    $(state, container).hide();
                                else
                                    $(state, container).show();
                                $(state, container).change(function() {
                                    if ($(this).val() == "00000000-0000-0000-0000-000000000000")
                                        $(city, container).hide();
                                    else
                                        new treeforms.binder({
                                            name: "城市列表" + city,
                                            container: container,
                                            tree: ACTION_GET_REGION_TREE($(this).val()),
                                            linkings: { from: "/ResponseData/Children", to: city, outputs: { from: ["GenericCategoryId", "Name"], to: "<option value='{0}'>{1}</option>"} },
                                            ready: function() {
                                                useDefault(city);
                                                if (cityid && cityid != "") {
                                                    $(city, container).val(cityid);
                                                    cityid = null;
                                                }
                                                if ($(city, container).children("option").length == 1)
                                                    $(city, container).hide();
                                                else
                                                    $(city, container).show();
                                            }
                                        });
                                }).triggerHandler("change");
                            }
                        });
                }).triggerHandler("change");
            }
        });
    }
    window.CURRENCY_SELECTOR = function(handle) {
        var currency = $(handle).attr("_value");
        new treeforms.binder({
            name: "货币类型列表",
            tree: ACTION_GET_TREE(null, "48D4E71F-B83E-DE11-9504-000000004188"),
            linkings: { from: "/ResponseData/Children", to: handle, outputs: { from: ["GenericCategoryId", "Name"], to: "<option value='{0}'>{1}</option>"} },
            ready: function() {
                if (currency && currency != "") {
                    $(handle).val(currency);
                    currency = null;
                }
                $(handle).triggerHandler("change");
            }
        });
    }

    window.INIT_VALIDATE = function() {
        //$("form").validate();
    }

})(jQuery);